{% extends "../main.html" %}

{% block title %}{{ _("Source Code Market") }}{% end %}

{% block header %}
<script src="/static/js/pages/upgrades/source_code_market.js"></script>
{% from tornado.options import options %}
{% end %}

{% block modals %}
    <div id="buy-source-code-modal" class="modal hide fade" style="display: none; ">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>
            <h3>{{ _("Buy Source Code") }}</h3>
        </div>
        <div class="modal-body">
            <form id="buy-source-code-form" class="form-horizontal" action="/source_code_market" method="post">
                {% raw xsrf_form_html() %}
                <input id="buy-source-code-uuid" type="hidden" name="box_uuid" value="" />
            </form>
            <p id="buy-source-code-dialog">
                {{ _("Are you sure you want to buy this code?") }}
            </p>
            <br />
        </div>
        <div class="modal-footer">
            <a href="#" class="btn" data-dismiss="modal">{{ _("Close") }}</a>
            <button id="buy-source-code-submit" class="btn btn-primary" type="button">
                {{ _("Buy") }}
            </button>
        </div>
    </div>
{% end %}

{% block content %}
    <div class="container">
        <h1>
            <i class="fa fa-fw fa-code"></i>
            {{ _("Source Code Market") }}
        </h1>
        <br />
        {% if errors is not None and len(errors) != 0 %}
            {% for error in errors %}
                <div class="alert alert-error">
                    <a class="close" data-dismiss="alert" href="#">&times;</a>
                    <h4 class="alert-heading">{{ _("ERROR") }}</h4>
                    {{ error }}
                </div>
            {% end %}
        {% end %}
        <div class="well">
            {% if len(boxes) == 0 %}
                <br />
                <h4 style="text-align:center;">{{ _("No Source Code For Sale at This Time") }}</h4>
                <br />
            {% else %}
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>#</th>
                            <th>{{ _("Box") }}</th>
                            <th>{{ _("Level") }}</th>
                            <th>{% if options.banking %}{{ _("Price") }}{% else %}{{ _("Cost") }}{% end %}</th>
                            <th>{{ _("File Name") }}</th>
                            <th>{{ _("Checksum") }}</th>
                            <th><!-- Buy/Download --></th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for index, box in enumerate(boxes) %}
                            <tr>
                                <td>{{ index + 1 }}</td>
                                <td>{{ box.name }}</td>
                                <td>{{ box.game_level.name }}</td>
                                <td>{% if options.banking %}{{ _("$") }}{% end %}{{ box.source_code.price }}</td>
                                {% if len(box.source_code.file_name) < 25 %}
                                    <td>{{ box.source_code.file_name }}</td>
                                {% else %}
                                    <td>{{ box.source_code.file_name[:25] + "..." }}</td>
                                {% end %}
                                <td>{{ box.source_code.checksum }}</td>
                                {% if box.source_code in user.team.purchased_source_code %}
                                    <td>
                                        <a id="download-source-code-button{{ index }}" class="btn btn-mini btn-success" data-toggle="modal" data-uuid="{{ box.uuid }}">
                                            <i class="fa fa-fw fa-download"></i>
                                            {{ _("Download") }}
                                        </a>
                                    </td>
                                {% else %}
                                    <td>
                                        <a id="buy-source-code-button{{ index }}" class="btn btn-mini" data-toggle="modal" href="#buy-source-code-modal" data-uuid="{{ box.uuid }}" data-price="{% if options.banking %}{{ _('$') }}{{ box.source_code.price }}{% else %}{{ box.source_code.price }} points{% end %}">
                                            {{ _("Buy Source Code") }}
                                        </a>
                                    </td>
                                {% end %}
                            </tr>
                        {% end %}
                    </tbody>
                </table>
            {% end %}
        </div>
    </div>
{% end %}